package com.party.web.biz.gatherInfo;

import com.party.common.utils.DateUtils;
import com.party.core.model.competition.CompetitionBusiness;
import com.party.core.model.gatherInfo.GatherInfoItinerary;
import com.party.core.model.gatherInfo.GatherInfoMemberOutput;
import com.party.web.biz.file.FileBizService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.util.List;

/**
 * @Author: Administrator.
 * @Description: 行程
 * @Date:Created in 2017/10/14 0014 下午 15:24.
 */
@Service
public class ItineraryBizService {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private FileBizService fileBizService;

    public SXSSFWorkbook exportExcel(List<GatherInfoMemberOutput> memberInfos, CompetitionBusiness competitionBusiness) throws FileNotFoundException {
        SXSSFWorkbook wb = new SXSSFWorkbook();
        Sheet sheet0 = wb.createSheet("信息收集导出");
        if (null != competitionBusiness) {
            appendCell3(memberInfos, sheet0);
        } else {
            appendCell2(memberInfos, sheet0);
        }
        return wb;
    }

    /**
     * 拼接单元格
     *
     * @param memberInfos 人员信息收集
     * @param sheet       sheet
     */
    public void appendCell3(List<GatherInfoMemberOutput> memberInfos, Sheet sheet) {
        String[] titles = new String[]{"项目名称", "小组编号", "组名", "组内职务", "昵称", "真实姓名", "性别", "公司", "职务", "手机", "微信",
                "城市", "行业", "基本信息审核状态", "证件类型", "身份证", "台胞证", "护照", "回乡证", "血型", "病史", "紧急联系人姓名",
                "紧急联系人关系", "紧急联系人手机", "保险信息审核状态", "出行方式", "出发班次", "到达站点", "到达时间", "到达是否需要接",
                "出行方式", "出发班次", "出发站点", "出发时间", "返程是否需要送", "备注"};


        Row titleRow = sheet.createRow(1);
        for (int i = 0; i < titles.length; i++) {
            Cell cell = titleRow.createCell(i);
            cell.setCellValue(titles[i]);
        }
        int count = 2;
        sheet.addMergedRegion(new CellRangeAddress((short) 0, (short) 0, (short) 0, (short) 11 + count));
        sheet.addMergedRegion(new CellRangeAddress((short) 0, (short) 0, (short) 12 + count, (short) 22 + count));
        sheet.addMergedRegion(new CellRangeAddress((short) 0, (short) 0, (short) 23 + count, (short) 27 + count));
        sheet.addMergedRegion(new CellRangeAddress((short) 0, (short) 0, (short) 28 + count, (short) 32 + count));

        Row titleRow0 = sheet.createRow(0);
        Cell cc0 = titleRow0.createCell(0);
        cc0.setCellValue("基本信息");
        Cell cc1 = titleRow0.createCell(12 + count);
        cc1.setCellValue("保险信息");
        Cell cc2 = titleRow0.createCell(23 + count);
        cc2.setCellValue("去程信息");
        Cell cc3 = titleRow0.createCell(28 + count);
        cc3.setCellValue("返程信息");

        for (int i = 0; i < memberInfos.size(); i++) {
            Row row = sheet.createRow(i + 1 + 1);
            GatherInfoMemberOutput output = memberInfos.get(i);

            String useTypeName = null;
            if (output.getUseCertificate() != null) {
                if (output.getUseCertificate() == 0) {
                    useTypeName = "身份证";
                } else if (output.getUseCertificate() == 1) {
                    useTypeName = "台胞证";
                } else if (output.getUseCertificate() == 2) {
                    useTypeName = "回乡证";
                } else if (output.getUseCertificate() == 3) {
                    useTypeName = "护照";
                }
            }

            String goTimeString = null;
            if (null != output.getGoTime()) {
                goTimeString = DateUtils.formatDate(output.getGoTime(), "yyyy-MM-dd HH:mm");
            }
            String backTimeString = null;
            if (null != output.getBackTime()) {
                backTimeString = DateUtils.formatDate(output.getBackTime(), "yyyy-MM-dd HH:mm");
            }
            if (null != output.getGoShuttle()) {
                output.setGoShuttleName(output.getGoShuttle() == 1 ? "是" : "否");
            }
            if (null != output.getBackShuttle()) {
                output.setBackShuttleName(output.getBackShuttle() == 1 ? "是" : "否");
            }

            String[] values = new String[]{
                    output.getProjectTitle(),
                    output.getGroupNo(),
                    output.getGroupName(), output.getGroupJobTitle(), output.getRealname(),
                    output.getFullname(), output.getSexName(), output.getCompany(),
                    output.getJobTitle(), output.getMobile(), output.getWechatNum(),
                    output.getCityName(), output.getIndustryName(), output.getBaseStatus(),
                    useTypeName, output.getIdCard(), output.getTaiwanCertificate(),
                    output.getPassport(), output.getReturnCard(), output.getBloodGroup(),
                    output.getMedicalHistory(), output.getContactName(), output.getContactRelation(),
                    output.getContactMobile(), output.getInsuranceStatus(),
                    output.getGoType(), output.getGoNumber(), output.getGoStation(),
                    goTimeString, output.getGoShuttleName(), output.getBackType(),
                    output.getBackNumber(), output.getBackStation(), backTimeString,
                    output.getBackShuttleName(), output.getRemarks()
            };

            for (int k = 0; k < values.length; k++) {
                Cell c0 = row.createCell(k);
                c0.setCellValue(values[k]);
            }
        }
    }

    /**
     * 拼接单元格
     *
     * @param memberInfos 人员信息收集
     * @param sheet       sheet
     */
    public void appendCell2(List<GatherInfoMemberOutput> memberInfos, Sheet sheet) {
        String[] titles = new String[]{"小组编号", "组名", "组内职务", "昵称", "真实姓名", "性别", "公司", "职务", "手机", "微信",
                "城市", "行业", "基本信息审核状态", "证件类型", "身份证", "台胞证", "护照", "回乡证", "血型", "病史", "紧急联系人姓名",
                "紧急联系人关系", "紧急联系人手机", "保险信息审核状态", "出行方式", "出发班次", "到达站点", "到达时间", "到达是否需要接",
                "出行方式", "出发班次", "出发站点", "出发时间", "返程是否需要送", "备注"};


        Row titleRow = sheet.createRow(1);
        for (int i = 0; i < titles.length; i++) {
            Cell cell = titleRow.createCell(i);
            cell.setCellValue(titles[i]);
        }
        int count = 1;
        sheet.addMergedRegion(new CellRangeAddress((short) 0, (short) 0, (short) 0, (short) 11 + count));
        sheet.addMergedRegion(new CellRangeAddress((short) 0, (short) 0, (short) 12 + count, (short) 22 + count));
        sheet.addMergedRegion(new CellRangeAddress((short) 0, (short) 0, (short) 23 + count, (short) 27 + count));
        sheet.addMergedRegion(new CellRangeAddress((short) 0, (short) 0, (short) 28 + count, (short) 32 + count));

        Row titleRow0 = sheet.createRow(0);
        Cell cc0 = titleRow0.createCell(0);
        cc0.setCellValue("基本信息");
        Cell cc1 = titleRow0.createCell(12 + count);
        cc1.setCellValue("保险信息");
        Cell cc2 = titleRow0.createCell(23 + count);
        cc2.setCellValue("去程信息");
        Cell cc3 = titleRow0.createCell(28 + count);
        cc3.setCellValue("返程信息");

        for (int i = 0; i < memberInfos.size(); i++) {
            Row row = sheet.createRow(i + 1 + 1);
            GatherInfoMemberOutput output = memberInfos.get(i);

            String useTypeName = null;
            if (output.getUseCertificate() != null) {
                if (output.getUseCertificate() == 0) {
                    useTypeName = "身份证";
                } else if (output.getUseCertificate() == 1) {
                    useTypeName = "台胞证";
                } else if (output.getUseCertificate() == 2) {
                    useTypeName = "回乡证";
                } else if (output.getUseCertificate() == 3) {
                    useTypeName = "护照";
                }
            }

            String goTimeString = null;
            if (null != output.getGoTime()) {
                goTimeString = DateUtils.formatDate(output.getGoTime(), "yyyy-MM-dd HH:mm");
            }
            String backTimeString = null;
            if (null != output.getBackTime()) {
                backTimeString = DateUtils.formatDate(output.getBackTime(), "yyyy-MM-dd HH:mm");
            }
            if (null != output.getGoShuttle()) {
                output.setGoShuttleName(output.getGoShuttle() == 1 ? "是" : "否");
            }
            if (null != output.getBackShuttle()) {
                output.setBackShuttleName(output.getBackShuttle() == 1 ? "是" : "否");
            }

            String[] values = new String[]{
                    output.getGroupNo(),
                    output.getGroupName(), output.getGroupJobTitle(), output.getRealname(),
                    output.getFullname(), output.getSexName(), output.getCompany(),
                    output.getJobTitle(), output.getMobile(), output.getWechatNum(),
                    output.getCityName(), output.getIndustryName(), output.getBaseStatus(),
                    useTypeName, output.getIdCard(), output.getTaiwanCertificate(),
                    output.getPassport(), output.getReturnCard(), output.getBloodGroup(),
                    output.getMedicalHistory(), output.getContactName(), output.getContactRelation(),
                    output.getContactMobile(), output.getInsuranceStatus(),
                    output.getGoType(), output.getGoNumber(), output.getGoStation(),
                    goTimeString, output.getGoShuttleName(), output.getBackType(),
                    output.getBackNumber(), output.getBackStation(), backTimeString,
                    output.getBackShuttleName(), output.getRemarks()
            };

            for (int k = 0; k < values.length; k++) {
                Cell c0 = row.createCell(k);
                c0.setCellValue(values[k]);
            }
        }
    }

    @SuppressWarnings("deprecation")
    public void appendCell(List<GatherInfoMemberOutput> memberInfos, Sheet sheet, Workbook wb) {
        String[] titles = new String[]{"组名", "组内职务", "昵称", "真实姓名", "性别", "公司", "职务", "手机", "微信",
                "城市", "行业", "个人介绍", "基本信息审核状态", "身份证", "台胞证", "护照", "回乡证", "使用的证件类型", "血型", "病史", "紧急联系人姓名",
                "紧急联系人关系", "紧急联系人手机", "保险信息审核状态"};

        String[] itineraryTitles = new String[]{
                "出行方式", "出发班次", "出发城市", "到达城市", "出发站点", "到达站点", "出发时间", "到达时间"
        };
        String[] roomTitles = new String[]{"官方安排住宿房间类型", "是否需要官方协助住宿", "住宿开始时间", "住宿结束时间", "房间类型", "备注"};
        Row titleRow = sheet.createRow(1);
        for (int i = 0; i < titles.length; i++) {
            Cell cell = titleRow.createCell(i);
            cell.setCellValue(titles[i]);
        }

        int goLength = titles.length + (itineraryTitles.length * 2);

        int j = 0;
        for (int i = titles.length; i < goLength; i++) {
            Cell cell = titleRow.createCell(i);
            cell.setCellValue(itineraryTitles[j]);
            j++;
            if (j == itineraryTitles.length) {
                j = 0;
            }
        }
        Cell cell = titleRow.createCell(goLength); // 35
        cell.setCellValue("到达是否需要接");

        j = 0;
        int backLength = goLength + 1 + itineraryTitles.length;
        for (int i = goLength + 1; i < backLength; i++) { // 36
            Cell cell1 = titleRow.createCell(i);
            cell1.setCellValue(itineraryTitles[j]);
            j++;
            if (j == itineraryTitles.length) {
                j = 0;
            }
        }
        Cell cell2 = titleRow.createCell(backLength);
        cell2.setCellValue("返程是否需要送");

        j = 0;
        int roomLength = backLength + 1 + roomTitles.length;
        for (int i = backLength + 1; i < roomLength; i++) {
            Cell cell1 = titleRow.createCell(i);
            cell1.setCellValue(roomTitles[j]);
            j++;
        }

        int count = 4;
        sheet.addMergedRegion(new CellRangeAddress((short) 0, (short) 0, (short) 2, (short) 11));
        sheet.addMergedRegion(new CellRangeAddress((short) 0, (short) 0, (short) 12, (short) 18 + count));
        sheet.addMergedRegion(new CellRangeAddress((short) 0, (short) 0, (short) 19 + count, (short) 26 + count));
        sheet.addMergedRegion(new CellRangeAddress((short) 0, (short) 0, (short) 27 + count, (short) 34 + count));
        sheet.addMergedRegion(new CellRangeAddress((short) 0, (short) 0, (short) 36 + count, (short) 43 + count));
        sheet.addMergedRegion(new CellRangeAddress((short) 0, (short) 0, (short) 45 + count, (short) 49 + count));

        Row titleRow0 = sheet.createRow(0);
        Cell cc0 = titleRow0.createCell(2);
        cc0.setCellValue("基本信息");
        Cell cc1 = titleRow0.createCell(12);
        cc1.setCellValue("保险信息");
        Cell cc2 = titleRow0.createCell(19 + count);
        cc2.setCellValue("去程信息-首段");
        Cell cc3 = titleRow0.createCell(27 + count);
        cc3.setCellValue("去程信息-第二段");
        Cell cc4 = titleRow0.createCell(36 + count);
        cc4.setCellValue("返程信息");
        Cell cc5 = titleRow0.createCell(45 + count);
        cc5.setCellValue("住宿信息");
        for (int i = 0; i < memberInfos.size(); i++) {
            Row row = sheet.createRow(i + 1 + 1);
            GatherInfoMemberOutput output = memberInfos.get(i);

            String useTypeName = null;
            if (output.getUseCertificate() != null) {
                if (output.getUseCertificate() == 0) {
                    useTypeName = "身份证";
                } else if (output.getUseCertificate() == 1) {
                    useTypeName = "台胞证";
                } else if (output.getUseCertificate() == 2) {
                    useTypeName = "回乡证";
                } else if (output.getUseCertificate() == 3) {
                    useTypeName = "护照";
                }
            }

            String[] values = new String[]{
                    output.getGroupName(), output.getGroupJobTitle(), output.getRealname(),
                    output.getFullname(), output.getSexName(), output.getCompany(),
                    output.getJobTitle(), output.getMobile(), output.getWechatNum(),
                    output.getCityName(), output.getIndustryName(), output.getIntroduction(),
                    output.getBaseStatus(), output.getIdCard(), output.getTaiwanCertificate(),
                    output.getPassport(), output.getReturnCard(), useTypeName, output.getBloodGroup(),
                    output.getMedicalHistory(), output.getContactName(), output.getContactRelation(),
                    output.getContactMobile(), output.getInsuranceStatus()
            };

            for (int k = 0; k < values.length; k++) {
                Cell c0 = row.createCell(k);
                c0.setCellValue(values[k]);
            }

            GatherInfoItinerary go = output.getGoItineraries().size() > 0 ? output.getGoItineraries().get(0) : null;
            if (go != null) {
                Cell travelType = row.createCell(titles.length);
                travelType.setCellValue(go.getTravelType()); //出行方式

                Cell travelNumber = row.createCell(titles.length + 1);
                travelNumber.setCellValue(go.getTravelNumber()); //出发班次

                Cell departCity = row.createCell(titles.length + 2);
                departCity.setCellValue(go.getDepartCity()); //出发城市

                Cell arrivalCity = row.createCell(titles.length + 3);
                arrivalCity.setCellValue(go.getArrivalCity()); //到达城市

                Cell departStation = row.createCell(titles.length + 4);
                departStation.setCellValue(go.getDepartStation()); //出发站点

                Cell arrivalStation = row.createCell(titles.length + 5);
                arrivalStation.setCellValue(go.getArrivalStation()); //到达站点

                Cell departTime = row.createCell(titles.length + 6);
                if (go.getDepartTime() != null) {
                    departTime.setCellValue(DateUtils.formatDate(go.getDepartTime(), "yyyy-MM-dd HH:mm")); //出发时间
                }

                Cell arrivalTime = row.createCell(titles.length + 7);
                if (go.getArrivalTime() != null) {
                    arrivalTime.setCellValue(DateUtils.formatDate(go.getArrivalTime(), "yyyy-MM-dd HH:mm")); //到达时间
                }

                Cell isNeedShuttle = row.createCell(titles.length + 16); // 到达是否需要接
                if (go.getIsNeedShuttle() != null) {
                    isNeedShuttle.setCellValue(go.getIsNeedShuttle() == 1 ? "是" : "否");
                }
            }
            GatherInfoItinerary go2 = output.getGoItineraries().size() > 1 ? output.getGoItineraries().get(1) : null;
            if (go2 != null) {
                Cell travelType = row.createCell(titles.length + 8);
                travelType.setCellValue(go2.getTravelType()); //出行方式

                Cell travelNumber = row.createCell(titles.length + 9);
                travelNumber.setCellValue(go2.getTravelNumber()); //出发班次

                Cell departCity = row.createCell(titles.length + 10);
                departCity.setCellValue(go2.getDepartCity()); //出发城市

                Cell arrivalCity = row.createCell(titles.length + 11);
                arrivalCity.setCellValue(go2.getArrivalCity()); //到达城市

                Cell departStation = row.createCell(titles.length + 12);
                departStation.setCellValue(go2.getDepartStation()); //出发站点

                Cell arrivalStation = row.createCell(titles.length + 13);
                arrivalStation.setCellValue(go2.getArrivalStation()); //到达站点

                Cell departTime = row.createCell(titles.length + 14);
                if (go2.getDepartTime() != null) {
                    departTime.setCellValue(DateUtils.formatDate(go2.getDepartTime(), "yyyy-MM-dd HH:mm")); //出发时间
                }

                Cell arrivalTime = row.createCell(titles.length + 15);
                if (go2.getArrivalTime() != null) {
                    arrivalTime.setCellValue(DateUtils.formatDate(go2.getArrivalTime(), "yyyy-MM-dd HH:mm")); //到达时间
                }
            }

            GatherInfoItinerary back = output.getBackItineraries().size() > 0 ? output.getBackItineraries().get(0) : null;
            if (null != back) {
                Cell travelType = row.createCell(titles.length + 17);
                travelType.setCellValue(back.getTravelType()); //出行方式

                Cell travelNumber = row.createCell(titles.length + 18);
                travelNumber.setCellValue(back.getTravelNumber()); //出发班次

                Cell departCity = row.createCell(titles.length + 19);
                departCity.setCellValue(back.getDepartCity()); //出发城市

                Cell arrivalCity = row.createCell(titles.length + 20);
                arrivalCity.setCellValue(back.getArrivalCity()); //到达城市

                Cell departStation = row.createCell(titles.length + 21);
                departStation.setCellValue(back.getDepartStation()); //出发站点

                Cell arrivalStation = row.createCell(titles.length + 22);
                arrivalStation.setCellValue(back.getArrivalStation()); //到达站点

                Cell departTime = row.createCell(titles.length + 23);
                if (back.getDepartTime() != null) {
                    departTime.setCellValue(DateUtils.formatDate(back.getDepartTime(), "yyyy-MM-dd HH:mm")); //出发时间
                }

                Cell arrivalTime = row.createCell(titles.length + 24);
                if (back.getArrivalTime() != null) {
                    arrivalTime.setCellValue(DateUtils.formatDate(back.getArrivalTime(), "yyyy-MM-dd HH:mm")); //到达时间
                }

                Cell isNeedShuttle = row.createCell(titles.length + 25); // 到达是否需要接
                if (back.getIsNeedShuttle() != null) {
                    isNeedShuttle.setCellValue(back.getIsNeedShuttle() == 1 ? "是" : "否");
                }
            }

            Cell publicRoomType = row.createCell(titles.length + 26);
            if (output.getPublicRoomType() != null) {
                publicRoomType.setCellValue(output.getPublicRoomType() == 1 ? "单间" : "标间");
            }
            Cell isNeedArrange = row.createCell(titles.length + 27);
            if (output.getIsNeedArrange() != null) {
                isNeedArrange.setCellValue(output.getIsNeedArrange() == 1 ? "是" : "否");
            }

            if (output.getIsNeedArrange() != null && output.getIsNeedArrange() == 1) {
                Cell roomStartTime = row.createCell(titles.length + 28);
                if (output.getRoomStartTime() != null) {
                    roomStartTime.setCellValue(DateUtils.formatDate(output.getRoomStartTime(), "yyyy-MM-dd")); //出发时间
                }

                Cell roomEndTime = row.createCell(titles.length + 29);
                if (output.getRoomEndTime() != null) {
                    roomEndTime.setCellValue(DateUtils.formatDate(output.getRoomEndTime(), "yyyy-MM-dd")); //出发时间
                }

                Cell privateRoomType = row.createCell(titles.length + 30);
                if (output.getPrivateRoomType() != null) {
                    privateRoomType.setCellValue(output.getPrivateRoomType() == 1 ? "单间" : "标间");
                }
            }

            Cell remarks = row.createCell(titles.length + 31);
            remarks.setCellValue(output.getRemarkInfo());
        }
    }

    /**
     * 生成二维码
     *
     * @param memberId  用户id
     * @param projectId 项目id
     */
    public void uploadQrCode(String memberId, String projectId) {
        try {
            String filePath = fileBizService.getUploadCIPath(memberId, "gatherInfo");

            String baseFileName = "base_" + projectId;
            String baseContent = "gather_info/base_info.html?projectId=" + projectId;
            fileBizService.uploadCloudOut(baseFileName, filePath, baseContent, memberId);

            String insuranceFileName = "insurance_" + projectId;
            String insuranceContent = "gather_info/insurance_info.html?projectId=" + projectId;
            fileBizService.uploadCloudOut(insuranceFileName, filePath, insuranceContent, memberId);

            String itineraryFileName = "itinerary_" + projectId;
            String itineraryContent = "gather_info/itinerary_info.html?projectId=" + projectId;
            fileBizService.uploadCloudOut(itineraryFileName, filePath, itineraryContent, memberId);
        } catch (Exception e) {
            logger.error("生成人员信息收集项目二维码异常");
        }
    }
}
